package chen.jia.xin.finalproject.ForegroundService.Util;

import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONObject;

import chen.jia.xin.finalproject.ForegroundService.gson.Weather;

public class Utility {
    //将返回的JSON数据解析成Weather实体类
    public static Weather handleWeatherResponse(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray jsonArray = jsonObject.getJSONArray("HeWeather");
            String weatherContent = jsonArray.getJSONObject(0).toString();
            //之前定义过相应的GSON实体类，就可以fromJson直接将JSON数据转换成Weather对象
            return new Gson().fromJson(weatherContent, Weather.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
